package com.tanhua.dubbo.server.controller;


import cn.hutool.core.convert.Convert;
import com.tanhua.common.pojo.UserFreeze;
import com.tanhua.dubbo.server.service.ManagerService;
import com.tanhua.dubbo.server.vo.BaseUserVo;
import com.tanhua.dubbo.server.vo.Message;
import com.tanhua.dubbo.server.vo.MsgParams;
import com.tanhua.dubbo.server.vo.PageResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@RequestMapping("manage")
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    /*
     *     消息通过
     * */
    @PostMapping("messages/pass")
    public ResponseEntity messagePass(@RequestBody String[] ids) {
        String msg = managerService.messagePass(ids);
        return ResponseEntity.ok(msg);
    }


    /*
     *   消息拒绝
     * */
    @PostMapping("messages/reject")
    public ResponseEntity rejectMessage(@RequestBody String[] ids) {
        String msg = managerService.rejectMessage(ids);
        return ResponseEntity.ok(msg);
    }

    /*
     *   撤销审核
     * */
    @PostMapping("messages/revocation")
    public ResponseEntity revocationMessage(@RequestBody String[] ids) {
        String msg = managerService.revocationMessage(ids);
        return ResponseEntity.ok(msg);
    }

    /*
     *    消息置顶
     * */
    @PostMapping("messages/{id}/top")
    public ResponseEntity topMessage(@PathVariable("id") String id) {
        String result = managerService.topMessage(id);
        return ResponseEntity.ok(result);
    }

    /*
     *    消息取消置顶
     * */
    @PostMapping("messages/{id}/untop")
    public ResponseEntity untopMessage(@PathVariable("id") String id) {
        String result = managerService.untopMessage(id);
        return ResponseEntity.ok(result);
    }

    /*
     * 消息翻页
     * */
    @GetMapping("messages")
    public ResponseEntity pageMessage(MsgParams msgParams
                                      ) {
        PageResult result = managerService.pageMessage(msgParams);
        return ResponseEntity.ok(result);
    }

    /*
     *   消息详情
     * */
    @GetMapping("messages/{id}")
    public ResponseEntity getMessage(@PathVariable("id") String id) {
        Message message = managerService.getMessage(id);
        return ResponseEntity.ok(message);
    }

    @PostMapping("users/freeze")
    public ResponseEntity freeze(@RequestBody UserFreeze userFreeze) {
        String message = managerService.userFreeze(userFreeze);
        return ResponseEntity.ok(message);
    }

    @PostMapping("users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody Map<String,Object> map){
        Long userId = Convert.toLong(map.get("userId"));
        String reasonsForThawing = map.get("reasonsForThawing").toString();
        String message=managerService.unfreeze(userId,reasonsForThawing);
        return ResponseEntity.ok(message);
    }

    @GetMapping("users")
    public ResponseEntity pageUsers(@RequestParam(value = "pagesize",defaultValue = "10") Integer pagesize,
                                    @RequestParam(value = "page",defaultValue = "1") Integer page,
                                    @RequestParam(value = "id",required = false) Long id,
                                    @RequestParam(value = "nickname",required = false) String nickname,
                                    @RequestParam(value = "city",required = false) String city){
        PageResult pageResult=managerService.queryByPage(page,pagesize,id,nickname,city);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("users/{userId}")
    public ResponseEntity baseUserInfo(@PathVariable("userId")Long userId){
        BaseUserVo result = managerService.findUserInfoById(userId);
        return ResponseEntity.ok(result);
    }



}
